package com.example.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.order.dto.OrdersDTO;
import com.example.order.entity.Orders;
import com.example.order.service.OrderService;
import com.example.user_api.UserClient;
import com.example.user_model.entity.User;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author sjz
 * @since 2024-07-08
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private UserClient userClient;

    /**
     * 移动端-个人中心-获取最新订单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page> userPage(int page, int pageSize) {
        log.info("page={},pageSize={}", page, pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //添加查询条件
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getCheckoutTime);
        //执行查询
        orderService.page(pageInfo, wrapper);

        return Result.success(pageInfo);
    }


    /**
     * 结算界面,提交订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {
        log.info("订单数据:{}", orders);
        orderService.submit(orders);
        return Result.success("下单成功");
    }

    /**
     * 后台订单明细分页查询,显示订单明细
     *
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("page={},pageSize={},name={},beginTime={},endTime={}",
                page, pageSize, number, beginTime, endTime);
        //LocalDate begin = LocalDate.parse(beginTime);
        //LocalDate end = LocalDate.parse(endTime);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //因为orders里面没有userName属性,创建一个ordersDto来扩展
        Page<OrdersDTO> ordersDtoPage = new Page<>();

        //添加查询条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        queryWrapper.between(beginTime != null && endTime != null, Orders::getCheckoutTime, beginTime, endTime);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);

        //执行查询
        orderService.page(pageInfo, queryWrapper);

        //把pageInfo拷贝到ordersDtoPage中
        BeanUtils.copyProperties(pageInfo, ordersDtoPage, "records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDTO> list = records.stream().map((item) -> {
            OrdersDTO ordersDTO = new OrdersDTO();
            //拷贝基本字段
            BeanUtils.copyProperties(item, ordersDTO);
            //用户id
            String userId = item.getUserId();
            //查询获取分类名称
            User user = userClient.getById(userId);
            String userName = user.getName();
            ordersDTO.setUserName(userName);
            return ordersDTO;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);
        return Result.success(ordersDtoPage);
    }


    /**
     * 后台-修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public Result<String> updateStatue(@RequestBody Orders orders){
        Orders order = orderService.getById(orders);
        order.setStatus(orders.getStatus());
        orderService.updateById(order);
        return Result.success("修改成功");
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public Result<String> again(@RequestBody Orders orders){
        log.info("再来一单...");
        log.info("orders:{}",orders.toString());
        orderService.again(orders);
        return Result.success("下单成功");
    }
}
